/**
 * 
 */
package co.cesarcompany.cesarmvc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import co.cesarcompany.cesarmvc.domain.Libro;
import co.cesarcompany.cesarmvc.service.LibroService;

/**
 * @author cesar
 *
 */
@Controller
@RequestMapping("/")
public class LibroController {
    
    private static int num = 3;
    
    @Autowired
    private LibroService libroService;
    
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        List<Libro> libros = libroService.findAll();
        model.addAttribute("libros", libros);
        model.addAttribute("libro", new Libro());
        return "index";
    }
    
    @RequestMapping("addLibro")
    public String addBook(@ModelAttribute("libro") Libro libro) {
        libro.setId(Long.valueOf(num));
        num++;
        libroService.insert(libro);
        return "redirect:/";
    }
}
